package weaver.interfaces.zhlx.duss.action.financemodule.sap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 提前支付申请流程领导审批节点提交后调用接口流水认领结果到SAP系统
 * @ModifyLog:
 **/
public class WeaTqzfsqlcSapAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaTqzfsqlcSapAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            SapUtil sapUtil = new SapUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            String jbrwb = Util.null2String(rs.getString("jbrwb"));
            JSONObject dataJson = new JSONObject();
            JSONObject headerJson = new JSONObject();
            headerJson.put("ZLIUCHENGID", lcbh);
            headerJson.put("USNAM", jbrwb);
            JSONArray zjjhList = new JSONArray();
            int dt1Count = 0;
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=? and (sappzh is null or trim(sappzh)='')", mainid);
            while (rs.next()) {
                JSONObject zjjhJson = new JSONObject();
                String zjjhxhwb = Util.null2String(rs.getString("zjjhxhwb"));
                zjjhJson.put("ZJIHUAID", zjjhxhwb);
                zjjhJson.put("BUKRS", Util.null2String(rs.getString("gsdm")));
                zjjhJson.put("POSID_EDIT", "");
                zjjhJson.put("HKONT", Util.null2String(rs.getString("wlkmbh")));
                zjjhJson.put("BELNR", Util.null2String(rs.getString("lzdjh")));
                zjjhJson.put("GJAHR", Util.null2String(rs.getString("yfpzhjnd")));
                zjjhJson.put("BUZEI", Util.null2String(rs.getString("yfpzxxmbh")));
                zjjhJson.put("RSTGR", Util.null2String(rs.getString("yydmxjlm")));
                zjjhJson.put("ZCONTRACTNO", Util.null2String(rs.getString("htbh")));
                zjjhJson.put("VBELN", Util.null2String(rs.getString("xsdd")));
                zjjhJson.put("EBELN", Util.null2String(rs.getString("cgdd")));
                zjjhJson.put("WRBTR", Util.null2String(rs.getString("ydzqysfyeyb")));
                zjjhJson.put("WAERS", Util.null2String(rs.getString("bz")));
                zjjhJson.put("ZZIJINMINGXI", Util.null2String(rs.getString("zjmxxmwb")));
                zjjhJson.put("ZJIZHANGFANGSHI", Util.null2String(rs.getString("jzfs")));
                String yksxfl = Util.null2String(rs.getString("yksxfl"));
                if ("6".equals(yksxfl) || "11".equals(yksxfl)) {
                    zjjhJson.put("KUNNR", Util.null2String(rs.getString("skdwdm")));
                } else {
                    zjjhJson.put("LIFNR", Util.null2String(rs.getString("skdwdm")));
                }

                JSONArray yhlsList = new JSONArray();
                rs1.executeQuery("select * from " + tablename + "_dt3 where mainid=? and zjjhxh=?", mainid, zjjhxhwb);
                while (rs1.next()) {
                    JSONObject yhlsJson = new JSONObject();
                    yhlsJson.put("SERIAL_NO", Util.null2String(rs1.getString("yxlsid")));
                    yhlsJson.put("ZUONR", Util.null2String(rs1.getString("pjbh")));
                    yhlsJson.put("ZFUKUANFANGSHI", Util.null2String(rs1.getString("fkxs")));
                    yhlsJson.put("BANKN", Util.null2String(rs1.getString("fkzh")));
                    yhlsJson.put("ZLIUSHUIFANGXIANG", 1);
                    yhlsJson.put("KUNNR", Util.null2String(rs1.getString("qsxx")));
                    yhlsJson.put("ZFBDT", Util.null2String(rs1.getString("dqr")).replace("-", ""));
                    yhlsJson.put("BUDAT", Util.null2String(rs1.getString("zfsj")).replace("-", ""));
                    yhlsJson.put("WRBTR", Util.null2String(rs1.getString("sfje")));
                    yhlsJson.put("WAERS", Util.null2String(rs1.getString("bz")));
                    yhlsJson.put("WRBTR1", Util.null2String(rs1.getString("ghje")));
                    yhlsJson.put("WAERS1", Util.null2String(rs1.getString("ghbz")));
                    yhlsList.add(yhlsJson);
                }
                zjjhJson.put("YHLS", yhlsList);
                zjjhList.add(zjjhJson);
            }
            headerJson.put("ZJJH", zjjhList);
            dataJson.put("HEADER", headerJson);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaTqzfsqlcSapAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            writeLog(">>>>>WeaTqzfsqlcSapAction>>>>>dataJson=" + dataJson);
            JSONObject resultJson = sapUtil.handleOKHttpRequest("FI012_1", dataJson, dataMap);
            writeLog(">>>>>WeaTqzfsqlcSapAction>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                String dataStr = outputJson.getString("DATA");
                JSONObject returnDataJson = JSONObject.parseObject(dataStr);
                JSONObject returnJson = JSONObject.parseObject(returnDataJson.getString("RETURN"));
                JSONArray returnZjjhList = JSONArray.parseArray(returnJson.getString("ZJJH"));
                for (int i = 0; i < returnZjjhList.size(); i++) {
                    String belnr = returnZjjhList.getJSONObject(i).getString("MSG_BELNR");
                    String bukrs = returnZjjhList.getJSONObject(i).getString("MSG_BUKRS");
                    String gjahr = returnZjjhList.getJSONObject(i).getString("MSG_GJAHR");
                    String zjihuaid = returnZjjhList.getJSONObject(i).getString("MSG_ZJIHUAID");
                    String sql = "update " + tablename + "_dt1 set sappzh=?,sapgsdm=?,hjnd=? where mainid=? and zjjhxhwb=?";
                    rs.executeUpdate(sql, belnr, bukrs, gjahr, mainid, zjihuaid);
                }
                if ("E".equals(msgType)) {
                    dt1Count++;
                    for (int i = 0; i < returnZjjhList.size(); i++) {
                        String mesg = returnZjjhList.getJSONObject(i).getString("MESG");
                        JSONArray mesgList = JSONArray.parseArray(mesg);
                        for (int j = 0; j < mesgList.size(); j++) {
                            String msgType1 = mesgList.getJSONObject(j).getString("MSG_TYPE");
                            if ("E".equalsIgnoreCase(msgType1)) {
                                message += mesgList.getJSONObject(j).getString("MSG_TEXT") + ";";
                            }
                        }
                    }
                }
            } else {
                dt1Count++;
                message += Util.null2String(resultJson.getString("message"));
            }
            if (dt1Count > 0) {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaTqzfsqlcSapAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}